package plugin;

import static org.junit.Assert.assertNotNull;

import java.util.List;
import java.util.ArrayList;

import Phase2.Node;

public class Broadcast implements Visitor
{

	protected ArrayList<Object> result;
	
	@SuppressWarnings("unchecked")
	@Override
	public Object visit(Node n, Object information) throws InterruptedException
	{
		assertNotNull(n);
		boolean rootAccess = false;
		//result = new ArrayList<Object>();
		BroadcastAlgorithm algorithm = new BroadcastAlgorithm();
		List<Node> nextNodes;
		Object temp;
		KeyValuePair<Integer, Object> newInfo;
		
		if(information instanceof KeyValuePair)
		{
			newInfo = (KeyValuePair) information;
			temp = n.acceptWithResponse(algorithm, ((KeyValuePair) information).getKey());
		}
		else
		{
			newInfo = new KeyValuePair<Integer, Object>(n.getId(rootAccess), information);
			temp = n.acceptWithResponse(algorithm, n.getId(rootAccess));
		}
		
		if(temp instanceof ArrayList)
		{
			nextNodes = (ArrayList<Node>) temp;
		}
		else
		{
			nextNodes = new ArrayList<Node>();		
		}
		
		for(Node node : nextNodes)
		{
			node.setPayload("message", newInfo.getValue());
			result = (ArrayList<Object>) node.acceptWithResponse(this, newInfo);
			getResponse(n, result);
		}
				
		return getResponse(n, result);
	}
	
	public Object getResponse(Node node, ArrayList<Object> result)
	{
		return null;
	}

}
